package com.jumi.microservice.material.controller;

import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.HttpStatus;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.exception.ExceptionSupply;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.domain.JmWarehouseSeq;
import com.jumi.microservice.material.dto.JmWarehouseSeqRequest;
import com.jumi.microservice.material.service.IJmWarehouseSeqService;
import com.jumi.microservice.material.service.JmWarehouseSeqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ssk
 * @create 2020/7/22 14:09
 **/
@RestController
@RequestMapping("/jmWarehouseSeq")
@Api(tags = {"【基础资料】仓库发货顺序接口"})
@Service
public class JmWarehouseSeqController extends BaseController implements IJmWarehouseSeqService{

    @Resource
    private JmWarehouseSeqService jmWarehouseSeqService;

    @PostMapping("/add")
    @ApiOperation("添加接口")
    @Log(title = "供应链管理-基础管理-仓库发货顺序",businessType = BusinessType.INSERT)
    public ResponseResult add(JmWarehouseSeqRequest jmWarehouseSeqRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        JmWarehouseSeq jmWarehouseSeq = new JmWarehouseSeq();
        try {
            jmWarehouseSeqRequest.clone(jmWarehouseSeq);
        } catch (Exception e) {
            throw new ExceptionSupply(500, "数据传递失败!").newException();
        }
        jmWarehouseSeq.setCreateBy(createBy);
        int rows = jmWarehouseSeqService.save(jmWarehouseSeq);
        return rows == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "包含已设置发货顺序的区域!") : ResponseResult.success(jmWarehouseSeq.getSeqId());
    }

   /* @GetMapping("/findById")
    public ResponseResult findById(Long seqId){
        JmWarehouseSeq seq= jmWarehouseSeqService.findById(seqId);
        return seq==null?ResponseResult.error("查无记录!"):ResponseResult.success(seq);
    }*/

    @GetMapping("/findById")
    @ApiOperation("根据ID查询接口")
    @ApiImplicitParam(name = "seqId", value = "仓库发货顺序ID")
    public ResponseResult findById(Long seqId) {
        JmWarehouseSeq seq = jmWarehouseSeqService.findById2(seqId);
        return seq == null ? ResponseResult.error("查无记录!") : ResponseResult.success(seq);
    }

    @PostMapping("/edit")
    @ApiOperation("编辑接口")
    @Log(title = "供应链管理-基础管理-仓库发货顺序",businessType = BusinessType.UPDATE)
    public ResponseResult edit(JmWarehouseSeqRequest jmWarehouseSeqRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmWarehouseSeq jmWarehouseSeq = new JmWarehouseSeq();
        try {
            jmWarehouseSeqRequest.clone(jmWarehouseSeq);
        } catch (Exception e) {
            throw new ExceptionSupply(500, "数据传递失败!").newException();
        }
        jmWarehouseSeq.setUpdateBy(updateBy);
        int rows = jmWarehouseSeqService.update(jmWarehouseSeq);
        return rows == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "包含已设置发货顺序的区域!") : ResponseResult.success(jmWarehouseSeq.getSeqId());
    }

    @PostMapping("/editStatus")
    @ApiOperation("修改状态接口")
    @Log(title = "供应链管理-基础管理-仓库发货顺序",businessType = BusinessType.UPDATE)
    public ResponseResult editStatus(JmWarehouseSeqRequest jmWarehouseSeqRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmWarehouseSeq jmWarehouseSeq = new JmWarehouseSeq();
        try {
            jmWarehouseSeqRequest.clone(jmWarehouseSeq);
        } catch (Exception e) {
            throw new ExceptionSupply(500, "数据传递失败!").newException();
        }
        jmWarehouseSeq.setUpdateBy(updateBy);
        int rows = jmWarehouseSeqService.updateStatus(jmWarehouseSeq);
        return ResponseResult.success("操作成功", jmWarehouseSeq.getSeqId());
    }

    @DeleteMapping("/{seqId}")
    @ApiOperation("删除接口")
    @ApiImplicitParam(name = "seqId", value = "仓库发货顺序ID")
    @Log(title = "供应链管理-基础管理-仓库发货顺序",businessType = BusinessType.DELETE)
    public ResponseResult delete(@PathVariable Long seqId) {
        return toResponse(jmWarehouseSeqService.delSeqBySeqId(seqId));
    }

   /* @GetMapping("/findByPage")
    public TableDataInfo<JmWarehouseSeq> findByPage(JmWarehouseSeq jmWarehouseSeq){
        //处理参数
        Set seqIds= jmWarehouseSeqService.findSeqIdsByParam(jmWarehouseSeq);
        if(seqIds!=null){
            jmWarehouseSeq.setSeqIds(seqIds);
        }
        startPage();
        List<JmWarehouseSeq> list= jmWarehouseSeqService.findAll(jmWarehouseSeq);
        return getDataTable(list);
    }*/

    @GetMapping("/findByPage")
    @ApiOperation("分页查询接口")
    public TableDataInfo<JmWarehouseSeq> findByPage(JmWarehouseSeqRequest jmWarehouseSeqRequest) {
        JmWarehouseSeq jmWarehouseSeq = new JmWarehouseSeq();
        try {
            jmWarehouseSeqRequest.clone(jmWarehouseSeq);
        } catch (Exception e) {
            throw new ExceptionSupply(500, "数据传递失败!").newException();
        }

        startPage();
        List<JmWarehouseSeq> list = jmWarehouseSeqService.findAll2(jmWarehouseSeq);
        return getDataTable(list);
    }

    @Override
    public ResponseResult findByAreaId(Long areaId) {
        List<Map<String, Object>> list = jmWarehouseSeqService.findByAreaId(areaId);
        return ResponseResult.success(list);
    }
}
